package com.lemon.reggie.controller;


import com.lemon.reggie.common.R;
import com.lemon.reggie.pojo.dto.AddCartDTO;
import com.lemon.reggie.pojo.dto.SubCartDTO;
import com.lemon.reggie.pojo.vo.ShoppingCartVO;
import com.lemon.reggie.service.ShoppingCartService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author Lemon
 * @since 2022-05-23
 */
@RestController
@RequestMapping("/shoppingCart")
@Validated
public class ShoppingCartController {

    @Resource
    private ShoppingCartService shoppingCartService;

    @PostMapping("/add")
    public R<?> addCart(@RequestBody @Valid AddCartDTO addCartDTO) {
        addCartDTO.setSetmealId(null);
        shoppingCartService.addCart(addCartDTO);
        return R.success("添加成功！");
    }

    @GetMapping("/list")
    public R<?> listCart() {
        List<ShoppingCartVO> shoppingCartVOList = shoppingCartService.listCart();
        return R.success(shoppingCartVOList);
    }

    @PostMapping("/sub")
    public R<?> subCart(@RequestBody SubCartDTO subCartDTO) {
        shoppingCartService.subCart(subCartDTO);
        return R.success("删除成功！");
    }

    @DeleteMapping("/clean")
    public R<?> cleanCart() {
        shoppingCartService.cleanCart();
        return R.success("清空购物车成功！");
    }
}

